package com.zthz.douxing.sendmessageservice.handlers

import com.zthz.douxing.event.UserWillDepartEvent
import com.zthz.douxing.event.UserWillTripWeatherJob
import com.zthz.douxing.sendmessageservice.utils.OrderXingeMessageService
import com.zthz.douxing.util.CommonUtilService
import com.zthz.douxing.utils.CommonUtil
import groovy.time.TimeCategory
import org.quartz.JobDataMap
import org.quartz.JobKey
import org.quartz.SimpleTrigger
import org.quartz.impl.triggers.SimpleTriggerImpl

import static org.quartz.JobBuilder.newJob

class WeatherMessageHanlderService extends AbstractMessageHandlerService{


    @Override
    protected void handle(JobDataMap jobDataMap) {
        UserWillDepartEvent event =  jobDataMap.get("userWillTripEvent")

        if(!event?.cityCode){
              return
        }
        if(!event.getuiId && !event.xgToken){
            return
        }

        UserWillDepartEvent userWillTripEvent = jobDataMap.get("userWillTripEvent")
        CommonUtilService commonUtilService = jobDataMap.get("commonUtilService")
        OrderXingeMessageService orderXingeMessageService = jobDataMap.get("orderXingeMessageService")

        String msgContent = orderXingeMessageService
                .orderWeatherMessage(userWillTripEvent)

        if(!msgContent){
            return
        }

        Date now = new Date()
        jobDataMap.put("msgContent",msgContent)
        jobDataMap.put("userWillTripEvent",userWillTripEvent)

        String weatherJob = CommonUtil.getJobKey("weatherMessage",
                event?.tripSegment?.plantrip?.personOwner?.id)
        String weatherTriggr = CommonUtil.getJobKey("arrivedTrigger",
                event?.tripSegment?.plantrip?.personOwner?.id)
        if(!weatherJob || !weatherTriggr)
            return

        Date isOutDate
        Date isLess10Minutes
        use(TimeCategory) {
            isOutDate = event.tripSegment.startTime+10.minutes
            isLess10Minutes =CommonUtil.getFromDuration2Date(
                    TimeCategory.minus(event.tripSegment.startTime,(now+10.minutes)))
        }

        if(isOutDate.getTime()>0 && isLess10Minutes.getTime()>0){
            Date trigDateTime
            Date isLess1Day
             use(TimeCategory) {
                 isLess1Day = CommonUtil.getFromDuration2Date(
                         TimeCategory.minus(event.tripSegment.startTime,(now+1.days))
                 )
              }

            if(isLess1Day.getTime()<=0){
                use(TimeCategory) {
                    trigDateTime = now+10.minutes
                }
            }else{
                use(TimeCategory) {
                    trigDateTime = event.tripSegment.startTime - 1.days
                }
            }

           def jobKey = new JobKey(
                   weatherJob,"UserWillTrip")
            def job = newJob(UserWillTripWeatherJob).withIdentity(jobKey).usingJobData(jobDataMap).build()
            SimpleTrigger trigger =
                    new SimpleTriggerImpl(weatherTriggr,trigDateTime, null, 0, 0L)
            quartzScheduler.deleteJob(jobKey)
            addJob(job,trigger)
        }
    }

}